package com.aaa.hospital.ManagementEnd.controller;

import com.aaa.hospital.ManagementEnd.pojo.Profit;
import com.aaa.hospital.ManagementEnd.pojo.Receipts;
import com.aaa.hospital.ManagementEnd.pojo.ReceiptsVo;
import com.aaa.hospital.ManagementEnd.service.Impl.ReceiptsSerImpl;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//订单页面
@Controller
@RequestMapping("/receipts")
public class ReceiptsCon {
    @Autowired
    private ReceiptsSerImpl receiptsSerImpl;
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    // 将java对象转为json格式的数据
    @ResponseBody
    //查询信息
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<Receipts> data = receiptsSerImpl.queryAll();
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }

    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> insert(Receipts receipts) {
        Date date=new Date();
        receipts.setShtime(date);
       int a= receipts.getJprice();
        int b= receipts.getPrice();
        int sum= receipts.getJnum();
        int count=(b-a)*sum;
        receipts.setRevenue(count);
        int result = receiptsSerImpl.insert(receipts);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }


    //搜索框查询
    @RequestMapping(name="查询订单信息",value = "/select_rec",method = RequestMethod.GET)
    @ResponseBody
    public String select_id_ypName(@Param("id") Integer id, @Param("ypname") String ypname){
        JSONObject json = new JSONObject();
        List<ReceiptsVo> data =receiptsSerImpl.select_rec(id,ypname);
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }


    @RequestMapping(name = "查询利润，生成统计图",value = "/profit",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getProfit(){
        List<Receipts> shops = receiptsSerImpl.selectProfit();
        Profit profit=new Profit();
        for (Receipts shop:shops) {
            List<String> x = profit.getX();
            List<Integer> y = profit.getY();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            x.add(sdf.format(shop.getShtime()));
            y.add(shop.getRevenue());
            profit.setX(x);
            profit.setY(y);
        }
        return profit;
    }
}
